<?php
use Base\AdminController;
use Illuminate\Support\Str;
use JetBrains\PhpStorm\NoReturn;

class DoTaskController extends AdminController
{
    protected string $table = 'do_tasks';

    protected string $model = "doTask";

    protected array $color = [
        1=>'danger',
        2=>'warning',
        3=>'primary',
        4=>'success',
        5=>'info',
    ];

    protected array $urgency_name = [
        1=>"紧急",
        2=>"重要",
        3=>"次要",
        4=>"一般",
        5=>"无优先级",
    ];

    #[NoReturn] public function init(): void
    {
        $this->routeWhiteLst = ['dotask@getoptions','dotask@getlst','dotask@add','dotask@edit'];
        parent::init();
    }

    #[NoReturn] public function getLstAction(): void
    {
        if (Request::input("type","dealt")=="dealt") {
            $data = DoTaskModel::query()
                ->join('users', 'do_tasks.user_id', '=', 'users.id')
                ->where('review_user_id', $this->member_id)
                ->selectRaw("ws_do_tasks.*,ws_users.nickname,ws_users.avatar")
                ->limit(4)
                ->get()->toArray();
            foreach ($data as $key => $vo) {
                $data[$key]['color'] = $this->color[$vo['urgency']];
                $start_time = time();
                $end_time = strtotime($vo['end_time']);
                $count_days = number_format((($end_time - $start_time) / 60 / 60 / 24), 1, '.') . "天后截止";
                $data[$key]['end_day'] = $count_days;
                $data[$key]['urgency_name'] = $this->urgency_name[$vo['urgency']];
            }
        }else if(Request::input("type")=="copy") {
            $data = $this->getSendCopy();
        }else {
            $data = $this->getMyApproval();
        }
        $this->apiSuccess($data, "获取成功", 20000);
    }

    protected function getSendCopy():?array
    {
        $data = ApprovalModel::query()
            ->join("users","users.id","=","approvals.user_id")
            ->join("organizations","organizations.id","=","approvals.org_id")
            ->join("flows","flows.id","=","approvals.flow_id")
            ->join("approval_cc_records","approval_cc_records.approval_id","=","approvals.id")
            ->where("approval_cc_records.recipient_id",$this->member_id)
            ->with(["user"=>function ($query) {
                $query->selectRaw("nickname,avatar")->orderBy("step_order","asc");
            }])
            ->selectRaw("ws_approvals.*,ws_users.nickname,ws_users.avatar,
                ws_organizations.title as org_name,ws_flows.title as flow_name")->orderBy("id","desc")
            ->limit(4)->get()->toArray();
        foreach ($data as $key=>$vo) {
            $data[$key]['status_d'] = ApprovalModel::$statusMap[$vo['status']];
            $data[$key]['status_color']  = match ($vo['status'])
            {
                0=>"primary",
                1=>"success",
                2=>"danger",
                -1=>"info",
                default => 'primary',
            };
            if ($vo['user']) {
                $data[$key]['approved_by'] = implode(";", array_column($vo['user'], 'nickname'));
            }
            unset($data[$key]['user']);
        }
        return $data;
    }

    protected function getMyApproval():?array
    {
        $data = ApprovalModel::query()
            ->join("users","users.id","=","approvals.user_id")
            ->join("organizations","organizations.id","=","approvals.org_id")
            ->join("flows","flows.id","=","approvals.flow_id")
            ->with(["user"=>function ($query) {
                $query->selectRaw("nickname,avatar")->orderBy("step_order","asc");
            }])
            ->where("approvals.user_id",$this->member_id)
            ->selectRaw("ws_approvals.*,ws_users.nickname,ws_users.avatar,
                ws_organizations.title as org_name,ws_flows.title as flow_name")
            ->orderBy("approvals.id","desc")
            ->limit(4)->get()->toArray();
        foreach ($data as $key=>$vo) {
            $data[$key]['status_d'] = ApprovalModel::$statusMap[$vo['status']];
            $data[$key]['status_color']  = match ($vo['status'])
            {
                0=>"primary",
                1=>"success",
                2=>"danger",
                -1=>"info",
                default => 'primary',
            };
            if ($vo['user']) {
                $data[$key]['approved_by'] = implode(";", array_column($vo['user'], 'nickname'));
            }
            unset($data[$key]['user']);
        }
        return $data;
    }
}
